package com.ym.excel.jfree;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * @Author: smuswc
 * @Date: 2018/2/7 14:37
 * @Description:
 */
public class YMJFreePieChart extends YMBaseChart {

    /**
     * JFreeChart jFreeChart = ChartFactory.createPieChart("饼图", getDataSet(), true, false,false);
     * @param jFreeChart
     * @param path
     * @param width
     * @param height
     * @return
     * @throws Exception
     */
    public String drawChart(JFreeChart jFreeChart, String path, Integer width, Integer height) throws Exception {
        // 获取生成图片路径
        path = getImagePath(path);

        jFreeChart.setBackgroundPaint(java.awt.Color.WHITE);
        PiePlot plot = (PiePlot)jFreeChart.getPlot();
        // 这只显示百分比
        plot.setCircular(true);
        plot.setLabelGap(0.02D);
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1}) {2}", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
        // 图例信息
        jFreeChart.getLegend().setItemFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));

        generateImage(jFreeChart, path, width, height);
        return path;
    }
}
